<?php
registerTable("user_login_data", new table_user_login());

class table_user_login extends Database
{

    public function __construct()
    {
        parent::__construct("user_login_data");
    }

    public function getSalt($uid)
    {
        return parent::fetch_assoc("uid=$uid", "salt")["salt"];
    }

    public function insert_data($profile)
    {
        return parent::insert($profile);
    }

    public function update_State($uid, $ip)
    {
        return parent::update(array(
            "lastlogindate" => "CURRENT_TIMESTAMP",
            "lastloginip" => $ip,
            "logintotal" => "logintotal+1"
        ));
    }

    public function contain_phone($phone)
    {
        parent::fetch_all("`phone`='$phone'", "uid");
        return ! parent::isEmpty();
    }

    public function contain_mail($mail)
    {
        parent::fetch_all("`mail`='$mail'", "uid");
        return ! parent::isEmpty();
    }

    public function fetch_uid_by_phone($phone)
    {
        $res = parent::fetch_assoc("`phone`='$phone'", "uid");
        if (parent::isEmpty())
            return - 1;
        return $res["uid"];
    }

    public function fetch_uid_by_mail($mail)
    {
        $res = parent::fetch_assoc("`mail`='$mail'", "uid");
        if (parent::isEmpty())
            return - 1;
        return $res["uid"];
    }

    public function fetch_ban_state($uid)
    {
        return parent::fetch_assoc("uid=$uid", "isbanned")["isbanned"];
    }

    public function fetch_session_id($uid)
    {
        return parent::fetch_assoc("uid=$uid", "sessionid")["sessionid"];
    }

    public function contain_uid($uid)
    {
        parent::fetch_all("`uid`='$uid'", "uid");
        return ! parent::isEmpty();
    }

    public function fetch_ban_state_by_uid_and_passwd($uid, $passwd)
    {
        $this->update_ban_state($uid);
        $res = parent::fetch_assoc("`uid`='$uid' AND `password`='$passwd'", "`uid`,`isbanned`");
        if (parent::isEmpty())
            return - 1;
        return $res["isbanned"];
    }

    public function update_ban_state($uid)
    {
        parent::update(array(
            "isbanned" => 0
        ), "`uid`='$uid' AND unix_timestamp(user_login_data.bantime)<=unix_timestamp(CURRENT_TIME)");
    }

    public function update_login($uid, $ip, $sessionid)
    {
        parent::update(array(
            "lastlogindate" => "CURRENT_TIME",
            "lastloginip" => $ip,
            "logintotal" => "user_login_data.logintotal+1",
            "sessionid" => $sessionid
        ), "`uid`='$uid'", array(
            "lastlogindate",
            "logintotal"
        ));
    }

    public function fetch_login_data_by_uid($uid)
    {
        return $this->fetch_assoc("`uid`=$uid", "`phone`,`mail`");
    }

    public function update_chenge_passwd($uid, $salt, $hash)
    {
        return $this->update(array(
            "salt" => $salt,
            "password" => $hash
        ), "`uid`=$uid");
    }

    public function fetch_sessionid_by_uid($uid)
    {
        return $this->fetch_assoc("`uid`=$uid", "`sessionid`")["sessionid"];
    }

    public function update_ban($uid, $state, $ban)
    {
        return $this->update(array(
            "isbanned" => $state,
            "bantime" => $ban
        ), "`uid`=$uid");
    }
}
?>